<?php
/**
 * maker: 梁康宁
 * Time: 2019-09-02 21:58:27
*/
namespace app\common\model;
use app\dlc\common\createHelper\Common;
use app\dlc\controller\CommonController;
use think\Model;
use think\Db;
class FormList extends Model
{
    public $typeList = ['head_botton'=>'<font color="green">头部按钮</font>','head_search'=>'<font color="#663399">头部搜索</font>','list_botton'=>'<font color="#fa8072">列表按钮</font>','list_title'=>'<font color="#6a5acd">列表标头</font>'];
	public function form(){
		return $this->hasOne('Form', 'id', 'form_id');
	}

    static function getList($id,$key){
        $data = explode('-',$id);
        $table = $data[0];
        $role = Db::name('form_list')->where(['table'=>$table])->value($key);
        $role = json_decode($role,true);
        return $role[$id];
    }

    static function setList($id,$value,$key){
        $data = explode('-',$id);
        $table = $data[0];
        $role = Db::name('form_list')->where(['table'=>$table])->value($key);
        $role = json_decode($role,true);
        $role[$id] = $value;
        $res = Db::name('form_list')->where(['table'=>$table])->update([$key=>json_encode($role)]);
        if ($res) return true;
        return false;
    }

    public function getIdsAttr($value,$data){
        $data = self::get($data['id']);
        $list = [];
        $a = ['head_botton','head_search','list_botton','list_title'];
        foreach ($a as $vv){
            foreach (explode(',',$data[$vv]) as $key=>$value){
                $list[] = $data['table'].'-'.$vv.'-'.$value;
            }
        }
        return $list;
    }

    public function getRoleAttr($value){
        return json_decode($value,true);
    }
    public function getTitleAttr($value){
        return json_decode($value,true);
    }

    public function getDataAttr($value){
	    $data = [];
        foreach ($this->ids as $key=>$id){
            $list = explode('-',$id);
            $table = $list[0];
            $type = $list[1];
            $key = $list[2];
            $a['id'] = $id;
            $a['type'] = $type;
            $a['typeText'] = $this->typeList[$type];
            $a['name'] = $key;
            $a['title'] = $this->getName($id);
            $a['role'] = $this->role[$id];
            $data [$id] = $a;
        }
        return $data;
    }






    static function getIds($id){
        $data = self::get($id);
        $list = [];
        $a = ['head_botton','head_search','list_botton','list_title'];
        foreach ($a as $vv){
            foreach (explode(',',$data[$vv]) as $key=>$value){
                $list[] = $data['table'].'-'.$vv.'-'.$value;
            }
        }
        return $list;
    }

    //type 类型。对应的字段
     function getName($id){
	    $title = $this->title;
	    if ($title[$id]) return $title[$id];
	    $list = explode('-',$id);
	    $table = $list[0];
	    $type = $list[1];
        $key = $list[2];
        return self::$type($key,$table);
    }

    static function head_botton($key,$table = ''){
        return \app\dlc\common\form\Form::$head_botton_array[$key];
    }
    static function list_botton($key,$table = ''){
        return \app\dlc\common\form\Form::$list_botton_array[$key];
    }

    static function head_search($key,$table = ''){
        $data = CommonController::get_db_column_comment($table);
        return $data[$key];
    }

    static function list_title($key,$table = ''){
        $data = CommonController::get_db_column_comment($table);
        return $data[$key];
    }






}
?>